import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Host, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';
import { AppComponent } from '../../../../app.component';
import { LongRunningTaskTypeEnum } from '../../../../framework/lrtm/lrtm.service';
import { ReportAnalytics } from '../../../../shared/models/googleAnalytics.model';
import { CompanyModel } from '../company.model';
import { CompanyService } from '../company.service';
import { CompanyReportDownloadModel } from '../shared/companyHeader/companyReportDownload/companyReportDownload.model';
import { CompanyGlobalCompactService, GCAssesment } from './companyGlobalCompact.service';
import { CompanyGCSummaryModel } from './models/companyGCsummary.model';

/**
 * Company global compact main component
 *
 * Author: Sorin Spiru
 * Date: 13.01.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'companyGlobalCompact.component.html',
    styleUrls: [ './companyGlobalCompact.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
    providers: [CompanyGlobalCompactService]
})
export class CompanyGlobalCompactComponent implements OnInit {
    public company: CompanyModel;
    public gcSummary: CompanyGCSummaryModel;
    public menuSub: Subscription;
    public gCAssessment: typeof GCAssesment = GCAssesment;
    public hasNonCompliant: boolean;
    public hasWatchlist: boolean;
    public downloadNonCompliantReport: CompanyReportDownloadModel;
    public downloadWatchlistReport: CompanyReportDownloadModel;
    private menuItems = [];
    public pillars: any[];
    public analyticsData: ReportAnalytics = new ReportAnalytics('Research', 'PDFGlobalCompactReport');

    constructor(
        private route: ActivatedRoute,
        private cdRef: ChangeDetectorRef,
        private companyService: CompanyService,
        private companyGlobalCompactService: CompanyGlobalCompactService,
        @Host() private rootComponent: AppComponent,
        ) {
            this.pillars = companyGlobalCompactService.getPillars();
    }

    ngOnInit() {
        this.companyService.company.subscribe(company => {
            this.company = company;
            this.companyService.setHeaderData(undefined);

            this.companyGlobalCompactService.getByCompany(this.company.id).subscribe(summary => {
                if(summary == null)
                {
                    this.gcSummary =  new CompanyGCSummaryModel(company.country);
                }
                else {
                    this.gcSummary = summary;
                    if (this.gcSummary.overallCompanyAssessment !== GCAssesment.Compliant) {
                        this.companyGlobalCompactService.getOverallPricipleData(this.company.id).subscribe(results => {
                            // set permissions to each list
                            this.hasNonCompliant = results[0] !== false;
                            const nonCompliantData = [].concat(results[0] || []);
                            this.hasWatchlist = results[1] !== false;
                            const watchlistData = [].concat(results[1] || []);
    
                            if (this.hasNonCompliant && nonCompliantData.length > 0) {
                                // set download data
                                this.downloadNonCompliantReport = new CompanyReportDownloadModel(
                                    LongRunningTaskTypeEnum.GccsReportPdf,
                                    'NonCompliantReport',
                                    {
                                        companyId: this.company.id,
                                        overallAssessment: GCAssesment['Non-Compliant']
                                    }
                                );
                            }
    
                            if (this.hasWatchlist && watchlistData.length > 0) {
                                // set download data
                                this.downloadWatchlistReport = new CompanyReportDownloadModel(
                                    LongRunningTaskTypeEnum.GccsReportPdf,
                                    'WatchlistReport',
                                    {
                                        companyId: this.company.id,
                                        overallAssessment: GCAssesment.Watchlist
                                    }
                                );
                            }
                            this.cdRef.markForCheck();
                        });
                    }
                }
                
                this.cdRef.markForCheck();
                this.rootComponent.finishLoading();
            });            
        });


        // create menu items for pillars
        this.pillars.forEach(pillar => {
            this.menuItems.push({
                title: pillar.name,
                path: ['globalcompact', 'pillar', pillar.id]
            });
        });

        // update routes data
        const pillarRoute = this.route.routeConfig.children.find(x => x.path === 'pillar/:id');
        pillarRoute.data.menuItems = this.menuItems;

        // notify the companyMenu component to display sub menu items
        this.companyService.menuItems.next(this.route.routeConfig.children);
        pillarRoute.data.menuItems = [];
    }
}
